/******************************************************************************
 * Copyright (C) 2017 Chengdu Dlhd information industry co. LTD.
 * All Rights Reserved.
 * 本软件为成都顶联互动信息产业有限公司开发研制。未经本公司正式书面同意，其他任何个人、团体不得使用、复制、修改或发布本软件.
 *****************************************************************************/
package net.dgg.dggcloud.service.jrcrm.service.cms;

import net.dgg.dggcloud.service.common.base.service.BaseService;
import net.dgg.dggcloud.service.common.exception.BizException;
import net.dgg.dggcloud.service.entity.user.User;
import net.dgg.dggcloud.service.jrcrm.dao.cms.NoticeDao;
import net.dgg.dggcloud.service.jrcrm.entity.cms.Notice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @ClassName: NoticeService
 * @Description:
 * @Author 汤宏
 * @date 2017/11/30 10:00
 */
@Service
public class NoticeService extends BaseService {
    @Autowired
    private NoticeDao noticeDao;
    /**
     * 查询公告
     * @param map
     * @return
     */
   public List<Map> listNoticeByMap(Map map){
        return noticeDao.listNoticeByMap(map);
    }

    /**
     * 查询公告总数
     * @param map
     * @return
     */
    public Integer countNoticeByMap(Map map){
        return noticeDao.countNoticeByMap(map);
    }

    /**
     * 根据公告ID查询公告
     * @param id
     * @return
     */
    public Notice getNoticeById(Long id){
        return this.repository.get(Notice.class,id);
    }

    /**
     * 新增或修改公告
     * @param notice
     * @param loger
     */
    public void saveOrUpdateNotice(Notice notice, User loger){
        try {
            if(null != notice.getId()){
                Notice noticeUp = this.repository.get(Notice.class, notice.getId());
                noticeUp.setTitle(notice.getTitle());
                noticeUp.setContent(notice.getContent());
                noticeUp.setUpdaterUser(loger);
                this.repository.saveOrUpdate(noticeUp);
            }else{
                long id = this.keyWorker.nextId();
                notice.setId(id);
                notice.setTitle(notice.getTitle());
                notice.setContent(notice.getContent());
                notice.setCreateUser(loger);
                this.repository.save(notice);
            }
        } catch (Exception e) {
            logger.error("编辑公告失败！" + e.getLocalizedMessage());
            throw new BizException("编辑公告失败！"+e.getLocalizedMessage(), 10001);
        }
    }

    /**
     * 根据ID删除公告
     * @param ids 如 1|2|3|4
     */
    public void deleteNoticeByIds(String ids){
        String[] arryIds = ids.split("\\|");
        for (int i = 0; i < arryIds.length; i++) {
            this.repository.deleteEntityById(Notice.class, Long.parseLong(arryIds[i]));
        }
    }
}
